<?php
require_once('dbinit.ini.php');

mb_internal_encoding("UTF-8");
date_default_timezone_set('Asia/Shanghai');

if (stristr($_SERVER['QUERY_STRING'], "';") || stristr($_SERVER['QUERY_STRING'], ";EXEC(")) {
    header("Location:/");
    exit;
}

if (!defined("SITE_ROOT")) {
    define("SITE_ROOT", $_SERVER['DOCUMENT_ROOT']);
}
global $active_request;
$active_request = false;
if($_SERVER['PHP_SELF']!="/ajax/auto_satellite.php" && $_SERVER['PHP_SELF'] != "/ajax/autogettip.php") {
    $active_request = true;
}

require_once(SITE_ROOT."/includes/tools/id.php");
require_once('Session.php');
global $gSession;

session_start();
//$url_session_id = $_GET['session_id'] ? $_GET['session_id'] : $_POST['session_id'];
//$url_session_id = $_GET['session_id'] ? $_GET['session_id'] : '';
/**
if($url_session_id && is_id_valid($_SESSION['G_USERID'])) {
    $res = $mysqli_read->query("SELECT * FROM `fk_user_sessionid` WHERE sessionid = '".$url_session_id."' and userid='".$_SESSION['G_USERID']."'");    
    if(!$res || $res->num_rows!=1) {
        header("Location:/html/404.html");
	error_log("ERROR: Session not found! (URL: ".$_SERVER['REQUEST_URI'].")");
        exit;
    }
    session_id($url_session_id);
    session_start();
}
if (!is_session_id_valid(session_id())) {
    error_log("session id '".session_id()."' not valid. Referer:".$_SERVER['HTTP_REFERER'].". User-Agent:".$_SERVER['HTTP_USER_AGENT'].". User:".$_SESSION['G_USERID'].". URL:.".$_SERVER['REQUEST_URI']);
    session_regenerate_id(true);
}
**/

// 处理用户的sessionid
if($_SESSION['G_USERID']!="") {
    if($active_request) {
        $session_res= $mysqli_read->query("SELECT sessionid FROM fk_user_sessionid WHERE userid='".$_SESSION['G_USERID']."'");
	
        // 查询出错或者没有查到相关记录，则直接返回
        if($session_res && $session_res->num_rows!=0 ) {		
            $row = $session_res->fetch_array(MYSQLI_ASSOC);
            if (session_id()!=$row['sessionid']) {				
                $mysqli_write->query("UPDATE fk_user_sessionid SET sessionid='".session_id()."' WHERE userid='".$_SESSION['G_USERID']."'");			
                if ($gSession) {
                    $gSession->setUser(session_id(), $_SESSION['G_USERID']);
                }
            }
        }
    }
}

header('Content-type: text/html; charset=utf-8');

// 设置smarty模板
require_once(SITE_ROOT."/libs/Smarty/Smarty.class.php");

// 创建 Smarty 对象
global $gSmarty;
$gSmarty = new Smarty;
if (!file_exists(SITE_ROOT.'/caches'))
{
    mkdir(SITE_ROOT.'/caches');
    chmod(SITE_ROOT.'/caches', 0755);
}
if (!file_exists(SITE_ROOT.'/compiled'))
{
    mkdir(SITE_ROOT.'/compiled');
    chmod(SITE_ROOT.'/compiled', 0755);
}
$gSmarty->template_dir   = SITE_ROOT.'/templates';
$gSmarty->cache_dir      = SITE_ROOT.'/caches';
$gSmarty->compile_dir    = SITE_ROOT.'/compiled';
$gSmarty->caching        = false;
$gSmarty->compile_force  = false;
$gSmarty->left_delimiter = '<{';
$gSmarty->right_delimiter = '}>';
//$gSmarty->plugins_dir[] = SITE_ROOT.'/includes/my_smarty_plugins';
//global $G_LANG;
// 页面当前所使用语言
//$G_LANG = "cn/";

// 用户所选择的皮肤,默认设置为1,当显示某个用户的页面是,会根据该用户的设置更新此变量值
//$G_SKIN = $_SESSION['G_USERSKIN'];
//if ($G_SKIN=="")
$G_SKIN = "skin4";

//$G_TPL = $_SESSION['G_USERTPL'];
//if ($G_TPL=="")
$G_TPL = "skin4";

//$gSmarty->assign("G_SKIN","skin4");
$gSmarty->assign("G_TPL",$G_TPL);



// 定义各模块数据的显示条=>结束
$G_SCAN_LIST=array("60X60","120X120","300X300","560X0");  // 图片的尺寸列表
$G_SESSIONTIMER=1800;				// 用户Session有效期,60S*30=1800S

// 根据不同的皮肤,引入不同的数据显示配置文件
//include_once(SITE_ROOT."/includes/config/".$G_LANG.$G_SKIN."_config.php");	

// 各项功能的开关标志 0:关 1:开
$sp_enable=1;
// 用户上传形象照时所生成的未剪切前的图片尺寸
$mainphoto_max_width = 400;
$mainphoto_max_height = 400;

// 私有图片目录,在审核时将不合法图片挪动到该目录下
$private_pic = "private_pic";


// 决定是否更新当前用户的session中的是否在线字段，如果是自动查询有无新小纸条则不更新	
if($_SESSION['G_USERID']) {
    apache_setenv("FACEKOO_USERNAME", $_SESSION['G_USERNAME']);
    if ($gSession) {
        $gSession->setUser(session_id(), $_SESSION['G_USERID']);
        if ($active_request) 
            $gSession->visit(session_id());
    }
}
function __autoload($class_name) {
    if (preg_match('/ActionDisplay$/',$class_name)) {
        require_once(SITE_ROOT."/includes/action_display/$class_name.php");
    } elseif (preg_match('/Component$/',$class_name)) {
        require_once(SITE_ROOT."/includes/component/$class_name.php");
    } elseif (preg_match('/Application$/', $class_name)) {
        require_once(SITE_ROOT."/includes/application/$class_name.php");
    } 
}
?> 
